#include "myhead.h"

void print_dir(const char *path,int level) //树状打印目录和文件
{
    //打开目录
    DIR *dir = opendir(path);
	char buf[256] = {};
    if(!dir){
        perror("opendir");
        return;
    }

    //读取目录
    struct dirent *dp = NULL;
    while(dp = readdir(dir)){
        //.和..要忽略
        if(strcmp(dp->d_name,".")==0||strcmp(dp->d_name,"..")==0)
            continue;
		int k;
		for(k=0;k<level;k++)
		{
			printf("|");
			printf("      "); 
		}
        //目录
        if(dp->d_type==DT_DIR){
            printf("|----%s\n",dp->d_name);//打印目录
            sprintf(buf,"%s/%s",path,dp->d_name);
            print_dir(buf,level+1);
        }																			
        else{
			printf("|----:%s\n",dp->d_name);
        }
    }

    //关闭目录
    closedir(dir);
}

int main(int argc,char *argv[])
{
    if(argc!=2){
        printf("%s dirpath\n",argv[0]);
        return -1;
    }

    print_dir(argv[1],0);

    return 0;
}